<template>
  <el-dialog v-model="visible" :title="form.id ? '修改' : '添加'" @closed="onReset" :close-on-click-modal="false">
    <el-form :model="form" ref="formRef" label-width="100px">
      <el-form-item label="类型">
        <el-select v-model="form.type" placeholder="请选择" clearable>
          <el-option :label="item.label" :value="item.value" v-for="item in extinguisherOption"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="型号">
        <el-input v-model="form.model" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="厂家">
        <el-input v-model="form.factory" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="维修机构">
        <el-input v-model="form.maintenance_organization" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="检验状态">
        <el-select v-model="form.status" placeholder="请选择">
          <el-option v-for="item in statusTypeOption" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="维修数量">
        <el-input v-model="form.quantity" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="维修完成数量">
        <el-input v-model="form.completed_quantity" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="维修时间">
        <el-date-picker v-model="form.maintenance_time" type="date" placeholder="选择日期"> </el-date-picker>
      </el-form-item>
      <el-form-item label="检验人">
        <el-input v-model="form.maintainer" placeholder="请输入"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="onConfirm">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { weixiujianyanAddOrUpdate } from '@/api/miehuoqi'
import { extinguisherOption, statusTypeOption } from '@/util/explain'
import { paraphrase } from 'toolkit-x'
const emit = defineEmits(['refresh'])
const visible = ref(false)
const formRef = ref()
const form = reactive({
  id: 0,
  maintenance_organization: '', //维修机构
  model: '', //灭火器型号
  factory: '', //厂家
  maintenance_time: '', //维修时间
  quantity: '', //维修数量
  completed_quantity: '', //维修完成数量
  status: '', //检验状态
  maintainer: '', //检验人
  type: ''
})

const resetForm = JSON.parse(JSON.stringify(form))
const open = (data?) => {
  visible.value = true
  if (data) Object.assign(form, { ...data })
}
const onConfirm = async () => {
  await formRef.value.validate()
  weixiujianyanAddOrUpdate(form).then(({ msg }: any) => {
    ElMessage.success(msg)
    visible.value = false
    emit('refresh')
  })
}
const onReset = () => {
  Object.keys(resetForm).forEach((item: string) => {
    form[item] = resetForm[item]
  })
}
defineExpose({ open })
</script>

<style lang="scss" scoped></style>
